//
//  HeaderView.m
//  7.14详情
//
//  Created by lovena on 15/7/15.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "XQHeaderView.h"
#import "MJExtension.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
@interface XQHeaderView()
@property(nonatomic,retain)NSArray *xqArray;
@property(nonatomic,strong)UIScrollView *bigScrollView;
@property(nonatomic,strong)UIScrollView *smallScrollView;
@property(nonatomic,strong)UIView *bigView;
@property(nonatomic,strong)UIButton *btn;
@property(nonatomic,strong)UIView *imageView;
@property(nonatomic,strong)UIButton *selectedBtn;//选中的前一个按钮

@end
@implementation XQHeaderView


+(id)initWithHeader
{
    
    return [[[NSBundle mainBundle]loadNibNamed:@"XQHeaderView" owner:nil options:nil] lastObject];
}

-(void)setData
{
    [self setScrollView];
    [self loadData];
    //移动按钮
    _selectedBtn=_activeBtn[0];
    _selectedBtn.selected=YES;
}
//进去详情页
- (IBAction)gotoDetail {
    if([self.delegate respondsToSelector:@selector(clickDetail)])
    {
        [self.delegate clickDetail];
    }
}

//滑动
-(void)setScrollView
{
    _scorllView.pagingEnabled=YES;
    _scorllView.showsHorizontalScrollIndicator=NO;
    //图片加入
    for(int i=0;i<self.model.image.count;i++)
    {
        
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(i*320, 0, 320, self.scorllView.frame.size.height)];
        image.userInteractionEnabled=YES;
        image.tag=i;
        [image setImageWithURL:[NSURL URLWithString:_model.image[i]]];
        [_scorllView addSubview:image];
        
        //点击事件
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bigImage)];
        [image addGestureRecognizer:tap];
    }
    _scorllView.contentSize=CGSizeMake(320*self.model.image.count, 0);
}
-(void)bigImage
{
    //代理
    if ([self.delegate respondsToSelector:@selector(hiddenNav:)]) {
        [self.delegate hiddenNav:YES];
    }
    
    
    _bigView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    _bigView.backgroundColor=[UIColor blackColor];
    [self addSubview:_bigView];
    
    //点击事件
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTouch)];
    tap.numberOfTapsRequired=2;
    [_bigView addGestureRecognizer:tap];

    //放个按钮
    _btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [_btn setBackgroundImage:[UIImage imageNamed:@"icon_bg_white"] forState:UIControlStateNormal];
    [_btn setImage:[UIImage imageNamed:@"back_big_icon"] forState:UIControlStateNormal];
    _btn.frame=CGRectMake(20, 10, 30, 30);
    [_btn addTarget:self action:@selector(returnMain) forControlEvents:UIControlEventTouchUpInside];
    _btn.alpha=0.3;
    [_bigView addSubview:_btn];
    //创建滑动
    _bigScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 49, 300, 300)];
    _bigScrollView.delegate=self;
    [_bigView addSubview:_bigScrollView];
    _bigScrollView.backgroundColor=[UIColor whiteColor];
    _bigScrollView.pagingEnabled=YES;
    _bigScrollView.showsHorizontalScrollIndicator=NO;
    
    //图片加入
    for(int i=0;i<self.model.image.count;i++)
    {
        
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(i*300, 0, 290, _bigScrollView.frame.size.height)];
        image.userInteractionEnabled=YES;
        [image setImageWithURL:[NSURL URLWithString:_model.image[i]]];
        [_bigScrollView addSubview:image];
    }
    _bigScrollView.contentSize=CGSizeMake(300*_model.image.count, 0);
    
    //小的滑动
    _smallScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(10, _bigScrollView.bottom+10, 300, 70)];
    _smallScrollView.decelerationRate=0.1;
    _smallScrollView.delegate=self;
    _smallScrollView.showsHorizontalScrollIndicator=NO;
    [_bigView addSubview:_smallScrollView];
    
    //背景图片
//    _imageView=[[UIView alloc]initWithFrame:CGRectMake(130, _bigScrollView.bottom+10, 70, 70)];
//    [_bigView addSubview:_imageView];
//    _imageView.backgroundColor=[UIColor orangeColor];
    
    
    XQModel *m2=self.xqArray[0];
    //图片加入
    int index=0;
    for(int i=0;i<_model.smallimage.count;i++)
    {
        
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(index+130, 5, 60, 60)];
        image.userInteractionEnabled=YES;
        [image setImageWithURL:[NSURL URLWithString:_model.smallimage[i]]];
        image.backgroundColor=[UIColor whiteColor];
        [_smallScrollView addSubview:image];
        [_bigView bringSubviewToFront:image];
        index+=70;
    }
    _smallScrollView.contentSize=CGSizeMake(130*2+_model.smallimage.count*70-10, 0);


}
//点击事件
-(void)didTouch
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    if (_btn.hidden) {
        _btn.hidden=NO;
    }
    else
    {
        _btn.hidden=YES;
    }
    [UIView commitAnimations];
}

-(void)returnMain
{
    //把bigview删除
    [_bigView removeFromSuperview];
    _bigView=nil;
    //显示分栏
    if ([self.delegate respondsToSelector:@selector(hiddenNav:)]) {
        [self.delegate hiddenNav:NO];
    }
    
}
//两个活动之间的连接
-(void)contentsScrollView:(UIScrollView*)scrollView
{
    XQModel *m=self.xqArray[0];
    int index;
    //如果是小的
    if(scrollView==_smallScrollView)
    {
        index=floor((scrollView.contentOffset.x-35)/70+1);
        [_bigScrollView setContentOffset:CGPointMake(index*300, 0) animated:YES];
    }
    else
    {
        index=scrollView.contentOffset.x/300;
    }
    //小滑动位置
    [_smallScrollView setContentOffset:CGPointMake(index*70, 0) animated:YES];
    if (index<0||index>m.image.count) {
        return;
    }

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView==_bigScrollView||scrollView==_smallScrollView) {
        [self contentsScrollView:scrollView];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //移动按钮
    if (scrollView==_scorllView) {
        int index=(scrollView.contentOffset.x+scrollView.width/2)/scrollView.width;
        _selectedBtn.selected=NO;
        _selectedBtn=_activeBtn[index];
        _selectedBtn.selected=YES;
    }
}

//给数据
-(void)loadData
{
    //介绍
    self.price.text=[NSString stringWithFormat:@"¥ %@",_model.price];
    // 名字
    [self.name setTitle:_model.name forState:UIControlStateNormal];
    //打折价钱
    self.oldprice.text=[NSString stringWithFormat:@"¥%@",_model.oldprice];
    self.know.text=_model.know;
    self.fuwu.text=_model.fuwu;
    self.hpl.text=_model.hpl;
    self.person.text=_model.person;
    for(int i=0;i<_model.pj.count;i++)
    {
        UILabel *label=self.pj[i];
        label.text = _model.pj[i];
        
    }
}

-(void)setModel:(XQModel *)model
{
    _model = model;

}
@end
